package com.sea.supply.controller;


import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.entity.CreateSku;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.product.service.CategoryService;
import com.sea.product.service.ChannelService;
import com.sea.supply.entity.QualityProduce;
import com.sea.supply.entity.QualityReport;
import com.sea.supply.service.BuyService;
import com.sea.supply.service.QualityProduceService;
import com.sea.supply.service.QualityReportService;
import com.sea.supply.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/quality/produce")
public class QualityProduceController {
    @Autowired
    private ChannelService channelService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private QualityReportService qualityReportService;
    @Autowired
    private QualityProduceService qualityProduceService;

    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("category",categoryService.findCategory());
        model.addAttribute("channel", channelService.findByCnd(null));
        model.addAttribute("supplier", supplierService.findByCnd(null));
        return "/sea/supply/quality/produce/main";
    }

    @RequestMapping("/edit")
    public String edit(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);

        return "/sea/supply/quality/produce/edit";
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                         @RequestParam(value = "offset", defaultValue = "0") int start,
                                     QualityProduce qualityProduce

    ) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<QualityProduce> list = qualityProduceService.findByCnd(qualityProduce);
        PageInfo<QualityProduce> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(QualityProduce qualityProduce) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != qualityProduce && qualityProduceService.usave(qualityProduce) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
